package com.zhaiweijia.eurekaconsumerribbon;

import com.netflix.loadbalancer.BestAvailableRule;
import com.netflix.loadbalancer.IRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class EurekaConsumerRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaConsumerRibbonApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @Bean
    public IRule myRule() {
        //return new RandomRule();//随机选取一个服务，进行访问
        //return new RoundRobinRule();//轮询方式选取一个服务，进行访问 默认方式
        //return  new RetryRule();//如果一个服务挂了，会尝试几次访问，如果几次访问失败后，下次就跳过失败的服务。
        //return new WeightedResponseTimeRule();//按照权重访问，根据响应时间计算权重，响应越长，权重越低，权重越低的服务器，被选择的可能性就越低
        //return new PredicateBasedRule();//先过滤清单再轮询
        return new BestAvailableRule(); //当高并发时，会选中最空闲的服务
    }


}
